package com.office.supplies.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.office.supplies.mapper.BuyMapper;
import com.office.supplies.mapper.SaleMapper;
import com.office.supplies.mapper.StockMapper;
import com.office.supplies.pojo.Buy;
import com.office.supplies.pojo.Stock;
import com.office.supplies.pojo.dto.SelectBuyListDTO;
import com.office.supplies.pojo.vo.BuyListVO;
import com.office.supplies.service.BuyService;
import com.office.supplies.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author admin
 * @Description TODO
 * @Date 2023/12/11/0:53
 */
@Service
public class BuyServiceImpl extends ServiceImpl<BuyMapper, Buy> implements BuyService {

    @Autowired
    private BuyMapper buyMapper;

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private SaleMapper saleMapper;

    /**
     * 采购商品
     *
     * @param buy Buy
     */
    @Transactional
    public void butGoods(Buy buy) {
        // 获取当前员工id
        Long empId = BaseContext.getCurrentId();
        buy.setEmpId(empId);
        // 设置为待审核
        buy.setStatus(1);

        // 向进货表新增进货信息
        buyMapper.insertBuy(buy);
    }

    /**
     * 删除采购信息
     *
     * @param buy Buy
     */
    @Transactional
    public void deleteBuyInfo(Buy buy) {

        // 有销售记录就不允许删除
//        if (saleMapper.selectSaleCountByGoodsId(buy.getGoodsId()) > 0) {
//            throw new RuntimeException("该商品已被销售无法删除！");
//        }

//        // 检查该商品的库存数量 ，可能会没有库存（销售完了会删除库存），没有库存就只用删除采购信息
//        Stock stock = stockMapper.getGoodsCountByGoodsId(buy.getGoodsId());
//
//        if (stock == null) {// 没有库存就只用删除采购信息
//            buyMapper.deleteById(buy.getId());
//            return;
//        }
//
//        // 总库存-采购信息中的商品数=删除采购信息后的库存量
//        int i = stock.getGoodsCount() - buy.getBuyCount();
//        if (i <= 0) {// 会导致库存 = 0
//            stockMapper.deleteStockByGoodsId(buy.getGoodsId());
//        } else {// 不会导致库存=0(i>0)，则直接修改库存，并删除采购订单即可
//            Stock s = Stock.builder()
//                    .goodsId(buy.getGoodsId())
//                    .goodsCount(stock.getGoodsCount() - buy.getBuyCount())
//                    .createDate(new Date())
//                    .build();
//            stockMapper.setGoodsCount(s);
//        }
        // 删除采购信息
        buyMapper.deleteById(buy.getId());

    }

    /**
     * 条件分页查询采购订单
     *
     * @param pageNum          Integer
     * @param pageSize         Integer
     * @param selectBuyListDTO SelectBuyListDTO
     * @return PageInfo<BuyListVO>
     */
    public PageInfo<BuyListVO> getBuyListByPageAndCondtion(Integer pageNum, Integer pageSize,
                                                           SelectBuyListDTO selectBuyListDTO) {
        PageHelper.startPage(pageNum, pageSize);
        List<BuyListVO> buyListVOList = buyMapper.selectBuyInfos(selectBuyListDTO);
        return new PageInfo<>(buyListVOList);
    }

    /**
     * 修改采购订单
     *
     * @param buy Buy
     */
    public void updateBuyInfo(Buy buy) {
        buyMapper.updateById(buy);
    }

    @Override
    @Transactional
    public void pass(Long id) {
        Buy buy = buyMapper.selectById(id);
        buy.setStatus(2);
        buyMapper.updateById(buy);

        // 同时更新或新增库存
        Stock s = stockMapper.getGoodsCountByGoodsId(buy.getGoodsId());

        if (s != null) {// 已有改商品在库存中，则选择更新库存
            Stock stock = Stock.builder()
                    .goodsId(buy.getGoodsId())
                    .goodsCount(buy.getBuyCount() + s.getGoodsCount())
                    .createDate(new Date())
                    .build();
            stockMapper.setGoodsCount(stock);
        } else {// 若库存中没有该商品，则选择新增库存表
            Stock stock = Stock.builder()
                    .goodsId(buy.getGoodsId())
                    .goodsCount(buy.getBuyCount())
                    .createDate(new Date())
                    .build();
            stockMapper.insertStock(stock);
        }
    }

}
